      $(document).ready(function(){
           // init datepicker
             $('[data-behaviour~=datepicker]').datepicker({
                    format: 'yyyy-mm-dd',
                    todayBtn: "linked",
                    todayHighlight: true,
                    endDate: '0d',
                    autoclose: true
                }).on('changeDate', dateChanged);
   
                $('[data-behaviour~=datepicker]').datepicker('setDate',getDateTimeISO8061());
      });
                

    //grab stories from ptracker every 
      setInterval(function(){
         RefreshOnClick()
      },1000000);

    //invoked when refresh-button is pushed
      function RefreshOnClick(){  
          dateFromPicker=$('[data-behaviour~=datepicker]').datepicker('getDate');
          var e = document.getElementById("ordertype");
          var ordertype = e.options[e.selectedIndex].value;
          //ajax call grab_entry action
           $.ajax({
            type: "GET",
            url: "/p_entries/grab_entry",
            dataType: "script" ,
            data: {date: dateFromPicker ,ordertype: ordertype}
          })
          //after grab_entry we need to call index again to reload the new entry
            $.ajax({
            type: "GET",
            url: "/index",
            dataType: "script" ,
            data: {date: dateFromPicker ,ordertype: ordertype}
          })
      }

    //date picker on datechanged call function
      function dateChanged(ev) {
       var dateFromPicker=$('[data-behaviour~=datepicker]').datepicker('getDate');  
       var e = document.getElementById("ordertype");
       var ordertype = e.options[e.selectedIndex].value;
       $.ajax({
          type: "GET",
          url: "/index",
          dataType: "script" ,
          data: {  date: dateFromPicker ,ordertype: ordertype}
        })
       .success(function(data){ 
          });
       return false; // prevents normal behaviour
      }


    //helper funciton
       function getDateSlash(){
                 var today = new Date();
                 var dd = today.getDate();
                 var mm = today.getMonth()+1; //January is 0!
                 var yyyy = today.getFullYear();
                 

                 if(dd<10) {
                     dd='0'+dd
                 } 
                 
                 if(mm<10) {
                     mm='0'+mm
                 } 
                 
                 today = yyyy+'/'+mm+'/'+dd;
                 return today
       }
       function getDateTimeISO8061(){
                 var today = new Date();
                 var dd = today.getDate();
                 var mm = today.getMonth()+1; //January is 0!
                 var yyyy = today.getFullYear();
                 

                 if(dd<10) {
                     dd='0'+dd
                 } 
                 
                 if(mm<10) {
                     mm='0'+mm
                 } 
                 
                 today = yyyy+'-'+mm+'-'+dd+"T00:00:00Z";
                 return today
       }

              





           
